<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Guild\GuildAccountDaily;
use App\Model\Guild\HostAccountDaily;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;

/**
 * 代理结算
 */
class GuildAccountDailyController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('代理结算日报表');
            $content->description('');
            $content->body($this->guild_grid());
        });
    }

    /**
     * 代理日报
     */
    protected function guild_grid()
    {
        return Admin::grid(GuildAccountDaily::class, function (Grid $grid) {
            // $field = DB::raw('*, (gift_turnover + video_turnover + voice_turnover + msg_turnover) as total_amount');
            $grid->model()->where(['flag' => 1])->orderBy('id', 'desc');
            $grid->dt('日期')->totalRow(function ($data) {
                return $this->buildRow('汇总：');
            });
            $grid->guildInfo()->guild_id('代理ID');
            $grid->guildInfo()->name('代理名');
            $grid->active_host_num('在线女用户');
            $grid->income_host_num('收益女用户');
            $grid->voice_turnover('语音流水')->totalRow(function ($data) {
                return $this->buildRow($this->t1 = $data);
            });
            $grid->video_turnover('视频流水')->totalRow(function ($data) {
                return $this->buildRow($this->t2 = $data);
            });
            $grid->msg_turnover('私信流水')->totalRow(function ($data) {
                return $this->buildRow($this->t3 = $data);
            });
            $grid->gift_turnover('礼物流水')->totalRow(function ($data) {
                return $this->buildRow($this->t4 = $data);
            });
            $grid->free_turnover('平台福利收入')->totalRow(function ($data) {
                return $this->buildRow($this->t5 = $data);
            });
            $grid->total_turnover('总流水（前5项）')->display(function ($data) {
                return $data + $this->free_turnover;
            })->totalRow(function ($data) {
                return $this->buildRow($this->t1 + $this->t2 + $this->t3 + $this->t4 + $this->t5);
            });
            $grid->guild_share_income('代理分成')->totalRow(function ($data) {
                return $this->buildRow($data);
            })->sortable();
            // $grid->host_share_income('女用户分成');
            // $grid->host_other_share('女用户其他分成')->display(function ($data) {
            //     return $this->host_total_share - $this->host_share_income;
            // });
            //禁用导出
            $grid->disableExport();
            $grid->disableCreateButton();
            //禁用行选择
            $grid->disableActions();
            $grid->disableRowSelector();
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    // $filter->date('dt', '日期');
                    $filter->between('dt', '日期')->date();
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->in('guildInfo.id', '所属代理')->multipleSelect('/get_guilds');
                });
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }

    function buildRow($amount = '')
    {
        return "<span class='text-danger text-bold'> {$amount} </span>";
    }

    /**
     * 女用户分成
     */
    public function host_share()
    {
        return Admin::content(function (Content $content) {
            $content->header('代理女用户日报表');
            $content->description('');
            $content->body($this->host_grid());
        });
    }

    /**
     * 女用户日报
     */
    protected function host_grid()
    {
        return Admin::grid(HostAccountDaily::class, function (Grid $grid) {
            $grid->model()->orderBy('dt', 'desc')->orderBy('guild_manage_id', 'desc');
            $grid->dt('日期');
            $grid->guildInfo()->guild_id('代理ID');
            $grid->guildInfo()->name('代理名');
            $grid->binded_at('签约日期');
            $grid->hostInfo()->user_profile_id('用户ID');
            $grid->hostInfo()->nick_name('昵称');
            $grid->hostInfo()->charm_value('魅力值');
            $grid->voice_turnover('语音流水');
            $grid->video_turnover('视频流水');
            $grid->msg_turnover('私信流水');
            $grid->gift_turnover('礼物流水');
            $grid->free_turnover('平台福利收入');
            $grid->total_turnover('总流水（前5项）')->display(function ($data) {
                return $this->msg_turnover + $this->voice_turnover + $this->gift_turnover + $this->video_turnover + $this->free_turnover;
            });
            $grid->host_share_income('总分成');
            $grid->disableCreateButton();
            //禁用行选择
            $grid->disableActions();
            $grid->disableRowSelector();
            // $grid->showFilterButton(false);
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->date('dt', '日期');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('guildInfo.id', '所属代理')->select('/get_guilds');
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('guildHost.host_profile_id', '女用户ID');
                });
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }
}
